﻿using System;
using System.Collections.Generic;
using UnityEngine;
using XUtliPoolLib;

namespace XMainClient
{
	// Token: 0x020008B0 RID: 2224
	internal class XCameraCollisonComponent : XComponent
	{
		// Token: 0x17002A35 RID: 10805
		// (get) Token: 0x0600866A RID: 34410 RVA: 0x0010F394 File Offset: 0x0010D594
		public override uint ID
		{
			get
			{
				return XCameraCollisonComponent.uuID;
			}
		}

		// Token: 0x17002A36 RID: 10806
		// (get) Token: 0x0600866B RID: 34411 RVA: 0x0010F3AC File Offset: 0x0010D5AC
		public Vector3 HitNormal
		{
			get
			{
				return this._hit_normal;
			}
		}

		// Token: 0x0600866C RID: 34412 RVA: 0x0010F3C4 File Offset: 0x0010D5C4
		public override void OnAttachToHost(XObject host)
		{
			base.OnAttachToHost(host);
			this._camera_host = (host as XCameraEx);
			this._transparent_update_count = 0;
		}

		// Token: 0x0600866D RID: 34413 RVA: 0x0010F3E2 File Offset: 0x0010D5E2
		public override void OnDetachFromHost()
		{
			base.OnDetachFromHost();
			this._camera_host = null;
		}

		// Token: 0x0600866E RID: 34414 RVA: 0x0010F3F4 File Offset: 0x0010D5F4
		private void InvisibleOn()
		{
			bool quality = XQualitySetting.GetQuality(EFun.ESceneFade);
			bool flag = this._invisible != null;
			if (flag)
			{
				for (int i = 0; i < this._invisible.Count; i++)
				{
					SceneMat sceneMat = this._invisible[i];
					bool flag2 = quality;
					if (flag2)
					{
						sceneMat.Fade(true);
						this._invisible[i] = sceneMat;
					}
					else
					{
						sceneMat.render.gameObject.layer = 31;
					}
				}
			}
		}

		// Token: 0x0600866F RID: 34415 RVA: 0x0010F47C File Offset: 0x0010D67C
		private void InvisibleOff()
		{
			bool quality = XQualitySetting.GetQuality(EFun.ESceneFade);
			bool flag = this._invisible != null;
			if (flag)
			{
				int i = 0;
				int count = this._invisible.Count;
				while (i < count)
				{
					SceneMat sceneMat = this._invisible[i];
					bool flag2 = sceneMat.render != null;
					if (flag2)
					{
						bool flag3 = quality;
						if (flag3)
						{
							sceneMat.Fade(false);
						}
						else
						{
							sceneMat.render.gameObject.layer = 0;
						}
					}
					i++;
				}
			}
			this._invisible.Clear();
		}

		// Token: 0x06008670 RID: 34416 RVA: 0x0010F519 File Offset: 0x0010D719
		public override void PostUpdate(float fDeltaT)
		{
			this.UpdateCamera(fDeltaT);
			this.UpdateSceneTransparent();
		}

		// Token: 0x06008671 RID: 34417 RVA: 0x0010F52C File Offset: 0x0010D72C
		protected void UpdateCamera(float fDeltaT)
		{
			this._hit_normal = Vector3.zero;
			this._real_pos = this._camera_host.CameraTrans.position;
			Vector3 vector = this._real_pos - this._camera_host.Anchor;
			float magnitude = vector.magnitude;
			this._layer_mask = 513;
			bool flag = XSingleton<XOperationData>.singleton.OperationMode == XOperationMode.X25D;
			if (flag)
			{
				this._layer_mask |= 4096;
			}
			RaycastHit raycastHit;
			bool flag2 = Physics.SphereCast(this._camera_host.Anchor, this._collision_radius, vector.normalized, out raycastHit, magnitude, this._layer_mask);
			if (flag2)
			{
				XOperationMode operationMode = XSingleton<XOperationData>.singleton.OperationMode;
				if (operationMode != XOperationMode.X25D)
				{
					if (operationMode - XOperationMode.X3D <= 1)
					{
						this._real_pos = raycastHit.point + raycastHit.normal * this._collision_radius;
						this._camera_host.Offset = Mathf.Min((this._camera_host.Anchor - this._real_pos).magnitude, magnitude);
						this._hit_normal = raycastHit.normal;
					}
				}
				else
				{
					bool flag3 = raycastHit.collider.gameObject.layer == 0 && XSingleton<XGame>.singleton.CurrentStage.Stage != EXStage.Hall && !Physics.SphereCast(this._camera_host.Anchor, this._collision_radius, vector.normalized, out raycastHit, magnitude, 512);
					if (flag3)
					{
						this._tdis = Mathf.Max((this._camera_host.Anchor - this._real_pos).magnitude, magnitude);
						this._camera_host.Offset += (this._tdis - this._camera_host.Offset) * Mathf.Min(1f, fDeltaT * 4f);
					}
					else
					{
						this._real_pos = raycastHit.point + raycastHit.normal * this._collision_radius;
						this._camera_host.Offset = Mathf.Min((this._camera_host.Anchor - this._real_pos).magnitude, magnitude);
						this._hit_normal = raycastHit.normal;
					}
				}
			}
			else
			{
				this._tdis = magnitude;
				this._camera_host.Offset += (this._tdis - this._camera_host.Offset) * Mathf.Min(1f, fDeltaT * 4f);
			}
			this._camera_host.CameraTrans.position = this._camera_host.Anchor - this._camera_host.CameraTrans.forward * this._camera_host.Offset;
			bool isLookAt = this._camera_host.IsLookAt;
			if (isLookAt)
			{
				this._camera_host.LookAtTarget();
			}
		}

		// Token: 0x06008672 RID: 34418 RVA: 0x0010F828 File Offset: 0x0010DA28
		protected void UpdateSceneTransparent()
		{
			bool flag = this._transparent_update_count <= 0;
			if (flag)
			{
				this._transparent_update_count = XCameraCollisonComponent._transparent_frequence;
				this._layer_mask = 1 << LayerMask.NameToLayer("45Transparent");
				this._real_pos = this._camera_host.Anchor - this._camera_host.CameraTrans.forward * this._camera_host.TargetOffset;
				Vector3 vector = this._real_pos - this._camera_host.Anchor;
				RaycastHit[] array = Physics.SphereCastAll(this._camera_host.Anchor, this._collision_radius, vector.normalized, vector.magnitude, this._layer_mask);
				bool flag2 = array.Length != 0 && !XSingleton<XCutScene>.singleton.IsPlaying;
				if (flag2)
				{
					this._hit_collider.Clear();
					for (int i = 0; i < array.Length; i++)
					{
						bool flag3 = array[i].collider.gameObject.layer == LayerMask.NameToLayer("45Transparent");
						if (flag3)
						{
							this._hit_collider.Add(array[i].collider);
						}
					}
					bool flag4 = !this.IsColliderSetEqual(this._hit_collider, this._last_hit_collider);
					if (flag4)
					{
						this.InvisibleOff();
						for (int j = 0; j < this._hit_collider.Count; j++)
						{
							IColliderRenderLinker colliderRenderLinker = this._hit_collider[j].gameObject.GetComponent("XColliderRenderLinker") as IColliderRenderLinker;
							bool flag5 = colliderRenderLinker != null;
							if (flag5)
							{
								foreach (Renderer renderer in colliderRenderLinker.GetLinkedRender())
								{
									bool flag6 = renderer != null && !this.InVisibleContains(renderer);
									if (flag6)
									{
										SceneMat item = default(SceneMat);
										item.InitRender(renderer);
										this._invisible.Add(item);
									}
								}
							}
						}
						this.InvisibleOn();
					}
					this.CloneColliderSet(this._last_hit_collider, this._hit_collider);
				}
				else
				{
					this.InvisibleOff();
					this._last_hit_collider.Clear();
				}
			}
			else
			{
				this._transparent_update_count--;
			}
		}

		// Token: 0x06008673 RID: 34419 RVA: 0x0010FA98 File Offset: 0x0010DC98
		private bool InVisibleContains(Renderer r)
		{
			for (int i = 0; i < this._invisible.Count; i++)
			{
				bool flag = this._invisible[i].render == r;
				if (flag)
				{
					return true;
				}
			}
			return false;
		}

		// Token: 0x06008674 RID: 34420 RVA: 0x0010FAE8 File Offset: 0x0010DCE8
		private bool IsColliderSetEqual(List<Collider> s, List<Collider> t)
		{
			bool flag = s.Count != t.Count;
			bool result;
			if (flag)
			{
				result = false;
			}
			else
			{
				for (int i = 0; i < s.Count; i++)
				{
					bool flag2 = !t.Contains(s[i]);
					if (flag2)
					{
						return false;
					}
				}
				result = true;
			}
			return result;
		}

		// Token: 0x06008675 RID: 34421 RVA: 0x0010FB44 File Offset: 0x0010DD44
		private void CloneColliderSet(List<Collider> s, List<Collider> t)
		{
			s.Clear();
			for (int i = 0; i < t.Count; i++)
			{
				s.Add(t[i]);
			}
		}

		// Token: 0x04002A00 RID: 10752
		public new static readonly uint uuID = XSingleton<XCommon>.singleton.XHash("Camera_Basic_Collison");

		// Token: 0x04002A01 RID: 10753
		private XCameraEx _camera_host = null;

		// Token: 0x04002A02 RID: 10754
		private float _collision_radius = 0.2f;

		// Token: 0x04002A03 RID: 10755
		private Vector3 _real_pos = Vector3.zero;

		// Token: 0x04002A04 RID: 10756
		private float _tdis = 0f;

		// Token: 0x04002A05 RID: 10757
		private List<SceneMat> _invisible = new List<SceneMat>();

		// Token: 0x04002A06 RID: 10758
		private List<Collider> _hit_collider = new List<Collider>();

		// Token: 0x04002A07 RID: 10759
		private List<Collider> _last_hit_collider = new List<Collider>();

		// Token: 0x04002A08 RID: 10760
		private int _layer_mask = 0;

		// Token: 0x04002A09 RID: 10761
		private Vector3 _hit_normal = Vector3.zero;

		// Token: 0x04002A0A RID: 10762
		private static int _transparent_frequence = 10;

		// Token: 0x04002A0B RID: 10763
		private int _transparent_update_count = 0;
	}
}
